//
//  YBDestinationTableViewcell.m
//  OurTourism
//
//  Created by 孟镇 on 15/11/24.
//  Copyright © 2015年 Devil_Coming. All rights reserved.
//

#import "YBDestinationTableViewcell.h"
#import "Header.h"
#import "BaseClassViewController.h"
@interface YBDestinationTableViewcell ()

@property (nonatomic, retain)UIImageView *imgGray;

@end
@implementation YBDestinationTableViewcell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.imgGray = [[UIImageView alloc]init];
        [self.contentView addSubview:self.imgGray];
        
        
        self.Bigimg = [[UIImageView alloc]init];
        [self.contentView addSubview:self.Bigimg];
        self.Bigimg.contentMode = UIViewContentModeScaleAspectFill;
        self.Bigimg.clipsToBounds = YES;
        
 
        
        self.titleLable = [[UILabel alloc]init];
        [self.contentView addSubview:self.titleLable];
        self.titleLable.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
        self.titleLable.shadowColor = [UIColor grayColor];
        self.titleLable.textColor = [UIColor colorWithRed:0.99 green:0.98 blue:0.97 alpha:1];
        self.titleLable.numberOfLines = 2;
        
        
        self.blueLable = [[UILabel alloc]init];
        self.blueLable.backgroundColor = [UIColor colorWithRed:0.41 green:0.78 blue:0.84 alpha:1];
        self.blueLable.layer.masksToBounds = YES;
        self.blueLable.layer.cornerRadius = 2;
        [self.contentView addSubview:self.blueLable];
        
        
        self.beginDateLable = [[UILabel alloc]init];
        [self.contentView addSubview:self.beginDateLable];
        self.beginDateLable.shadowColor = [UIColor grayColor];
        self.beginDateLable.textColor = [UIColor colorWithRed:0.99 green:0.98 blue:0.97 alpha:1];
        self.beginDateLable.font = [UIFont systemFontOfSize:12];
        
        
        self.dayCountLable = [[UILabel alloc]init];
        [self.contentView addSubview:self.dayCountLable];
        self.dayCountLable.shadowColor = [UIColor grayColor];
        self.dayCountLable.textColor = [UIColor colorWithRed:0.99 green:0.98 blue:0.97 alpha:1];
        self.dayCountLable.font = [UIFont systemFontOfSize:12];
        
        
        self.placeLable = [[UILabel alloc]init];
        [self.contentView addSubview:self.placeLable];
        self.placeLable.shadowColor = [UIColor grayColor];
        self.placeLable.textColor = [UIColor colorWithRed:0.99 green:0.98 blue:0.97 alpha:1];
        self.placeLable.font = [UIFont systemFontOfSize:12];
        

        self.userPhotoImg = [[UIImageView alloc]init];
        [self.contentView addSubview:self.userPhotoImg];
        
        
        self.userNameLabel = [[UILabel alloc]init];
        [self.contentView addSubview:self.userNameLabel];
        self.userNameLabel.shadowColor = [UIColor grayColor];
        self.userNameLabel.textColor = [UIColor colorWithRed:0.99 green:0.98 blue:0.97 alpha:1];
        self.userNameLabel.font = [UIFont systemFontOfSize:12];
        
        
        
        
        
    }
    return self;
}

//计算文本高度的方法
+ (CGFloat)heightWithText:(NSString *)text fontSize:(CGFloat)fontsize Width:(CGFloat)width

{
    //设置一个字典 保存文本属性
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontsize]};
    
    //预设尺寸 文本最大不会超过这个范围
    CGSize size = CGSizeMake(width, 2000);
    //根据文本信息 计算得到一个CGRect
    //参数1. 尺寸范围
    //参数2. 获取文本尺寸的方式
    //参数3. 文本属性
    //参数4. 无用信息(上下文)
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    // 背景图
    [self.Bigimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.imgGray).offset(10);
        make.top.mas_equalTo(self.imgGray).offset(5);
        make.size.mas_equalTo(CGSizeMake(WIDTH - 20, 240));
    }];
    
    self.Bigimg.layer.cornerRadius =5;
    self.Bigimg.layer.masksToBounds = YES;
    //自适应标题
   CGFloat HTitleLable = [[self class]heightWithText:self.titleLable.text fontSize:18 Width:WIDTH -20 - 20];
    [self.titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.Bigimg).offset(10);
        make.top.mas_equalTo(self.Bigimg).offset(15);
        make.size.mas_equalTo(CGSizeMake(WIDTH- 20, HTitleLable));
    }];
   //日期
    [self.beginDateLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.Bigimg).offset(20);
        make.top.mas_equalTo(self.titleLable).offset(HTitleLable + 10);
        make.size.mas_equalTo(CGSizeMake(WIDTH / 4, 15));
    }];
   //天数
    [self.dayCountLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.beginDateLable);
        make.left.mas_equalTo(self.beginDateLable).offset(10 + WIDTH / 4);
        make.size.mas_equalTo(CGSizeMake(WIDTH / 3, 15));
    }];
    //位置
    [self.placeLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.beginDateLable);
        make.top.mas_equalTo(self.beginDateLable).offset(20);
        make.size.mas_equalTo(CGSizeMake(WIDTH / 3, 15));
    }];
    //蓝条
    [self.blueLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLable);
        make.top.mas_equalTo(self.beginDateLable).offset(3);
        make.size.mas_equalTo(CGSizeMake(5, 30));
    }];
    
    //头像
    [self.userPhotoImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.Bigimg).offset(- 10);
        make.left.mas_equalTo(self.blueLable);
        make.size.mas_equalTo(CGSizeMake(WIDTH / 6, WIDTH / 6));
    }];
    self.userPhotoImg.layer.masksToBounds = YES;
    self.userPhotoImg.layer.cornerRadius = WIDTH/12;
    

//    
    [self.userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.userPhotoImg).offset(WIDTH/6+5);
        make.bottom.mas_equalTo(self.Bigimg).offset(-20);
        make.size.mas_equalTo(CGSizeMake(150, 30));
    }];
    
    
    
}

- (void)setTravelModel:(YBDestinationModel *)travelModel
{
    
    [self.Bigimg sd_setImageWithURL:[NSURL URLWithString:travelModel.cover_image] placeholderImage:nil];
    //[self.Bigimg sd_setImageWithURL:[NSURL URLWithString:_travelModel.cover_image] placeholderImage:nil];
    
    self.titleLable.text = travelModel.name;
    self.beginDateLable.text = travelModel.first_day;
    
    NSString *str = [NSString stringWithFormat:@"%@ %@", travelModel.day_count, @"天"];
    self.dayCountLable.text = str;
    
    self.placeLable.text = travelModel.popular_place_str;
    
}
- (void)setTravelUserModel:(YBDestinationModel *)travelUserModel
{
    
    [self.userPhotoImg sd_setImageWithURL:[NSURL URLWithString:travelUserModel.avatar_m] placeholderImage:nil];
    self.userNameLabel.text =[NSString stringWithFormat:@"%@ %@", @"by", travelUserModel.name];
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
